﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Interface.Models;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CNKI.TPI.Web.Interface.Areas.Resource.Controllers
{
    public class GetDBPublishedFieldController : BaseApiController
    {
        private ISingleDBPublishService singleService = null;
        private ICategoryDAO categoryDAO = null;
        public GetDBPublishedFieldController(ISingleDBPublishService singleService, ICategoryDAO categoryDAO)
        {
            this.singleService = generator.CreateInterfaceProxyWithTarget<ISingleDBPublishService>(singleService, new IInterceptor[] { new LogInterceptor() });
            this.categoryDAO = generator.CreateInterfaceProxyWithTarget<ICategoryDAO>(categoryDAO, new IInterceptor[] { new LogInterceptor() });
        }

        /// <summary>
        /// 获取数据库中对应的字段
        /// </summary>
        /// <param name="DbCode">数据库编码</param>
        /// <returns></returns>
        public object Get(string dbcode, string type)
        {
            OutputEntity result = new OutputEntity();
            try
            {
                SYS_CMS_DATABASE dbinfo = categoryDAO.GetDatabaseByCode(dbcode);
                if (dbinfo != null)
                {
                    IList<SYS_CMS_DATABASE_PUBLISH_FIELD> list = singleService.GetField(dbinfo.SysID.ToString(), (FieldType)int.Parse(type));
                    if (list != null)
                    {
                        result.Status = Status.Success;
                        result.data = list;
                    }
                    else
                    {
                        result.Status = Status.Error;
                    }
                }
                else
                {
                    result.Status = Status.Error;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result.Status = Status.Error;
                result.message = ex.Message;
            }
            return result;
        }
    }
}
